package org.siberia.client;

import org.siberia.client.bean.ActionSuiteDTO;
import org.siberia.client.model.ActionSuiteModel;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.ScrollContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.HasText;

public class ActionSuiteFormPanel extends FormPanel implements HasText
{
	private FormData formData;  

    private FormBinding binding = new FormBinding(this);  

    private FormButtonBinding buttonBinding = new FormButtonBinding(this); 
    
    private ActionList actionList = null;
    
    private Button addNewAction = null;
    
    private SelectionListener<ButtonEvent> addActionListener = null;
	  
	public ActionSuiteFormPanel()
	{	
		this.addActionListener = new SelectionListener<ButtonEvent>()
		{
			@Override
			public void componentSelected(ButtonEvent ce)
			{
				if ( actionList != null )
				{
					actionList.addNewAction();
				}
			}
		};
	}

	protected void onRender(Element parent, int index)
	{  
	    super.onRender(parent, index);
	    
	    formData = new FormData("-20");
	    
		// name
		TextField<String> fName = new TextField<String>();  
		fName.setFieldLabel("Name");  
		fName.setAllowBlank(false);  
//	    firstName.getFocusSupport().setPreviousId(simple.getButtonBar().getId());  
	    this.add(fName, formData); 

	    // description
	    TextArea fDescription = new TextArea();  
	    fDescription.setPreventScrollbars(true);  
	    fDescription.setFieldLabel("Description");  
	    this.add(fDescription, formData);
	    
	    this.addNewAction = new Button("Add");
	    this.add(this.addNewAction, formData);
	    this.addNewAction.setSize(50, 20);
	    this.addNewAction.addSelectionListener(this.addActionListener);

	    this.actionList = new ActionList(850, 570);
	    this.actionList.setText("Actions list");
	    this.actionList.setSize(920, 600);
	    this.add(this.actionList, formData);
	    
	    // buttons
	    Button b = new Button("Submit");  
	    this.addButton(b);  
	    this.addButton(new Button("Cancel"));  
	  
	    this.setButtonAlign(HorizontalAlignment.CENTER);
	    this.buttonBinding.addButton(b);  
	    
	    
	    this.binding.addFieldBinding(new FieldBinding(fName, "name"));  
	    this.binding.addFieldBinding(new FieldBinding(fDescription, "description"));
	    
	    this.binding.autoBind();

		//this.setSize(700, 700);  
	}
	
	public void bind(ActionSuiteModel model)
	{
	    this.binding.bind(model);
	}

	@Override
	public String getText()
	{
		return this.getHeading();
	}

	@Override
	public void setText(String text)
	{	
		this.setHeading(text);
	}
}
